<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               v-model:page="page"
               v-model:search="search"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               >
      <template  #menu-left>
        <el-button type="primary" icon="el-icon-plus" v-if="permission.order_edit" @click="handleAdd">新 增
        </el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   v-if="permission.order_edit"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template #menu="{ row, size, type }">
        <el-tooltip content="查看" placement="top">
          <el-button
                  icon="el-icon-view"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleView(row)"
                  v-if="permission.order_view"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="修改" placement="top">
          <el-button
                  icon="el-icon-edit"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleEdit(row)"
                  v-if="permission.order_edit"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button
                  icon="el-icon-delete"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowDel(row)"
                  v-if="permission.order_edit"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
    <order-item-form  ref="orderItemForm" @success="setMaterial"/>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,findList} from "@/api//anwise/order/order_item";
  import {mapGetters} from "vuex";
  import orderItemForm from './order_item_form.vue';

  export default {
      components: {
          orderItemForm
      },
    data() {
      return {
          customerId:'',
          idNo:'',
          salesDate:'',
          form: {},
          query: {},
          loading: false,
          page: {
              pageSize: 10,
              currentPage: 1,
              total: 0
          },
          selectionList: [],
          search:{},
          option: {
              height:'250px',
              calcHeight: 30,
              tip: false,
              searchShow: false,
              searchMenuSpan: 6,
              searchSpan:8,
              border: true,
              index: true,
              selection: true,
              reserveSelection:true,
              searchLabelWidth:80,
              indexWidth:100,
              labelWidth: 150,
              menuWidth: 120,
              addBtn: false,
              editBtn: false,
              viewBtn: false,
              delBtn: false,
              stripe: true,
              searchIndex: 3,
              searchIcon: false,
              menu:true,
          column: [
            {
              label: "订单编号",
              prop: "idNo",
              type: "input",
                fixed: true,
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "订单项",
              prop: "itemNo",
              type: "input",
                fixed: true,
                overHidden: true,
                sortable: true,
                width:120,
              rules: [{
                required: true,
                message: "请输入订单项",
                trigger: "blur"
              }],
            },
            {
              label: "物料编号",
              prop: "materialNo",
              type: "input",
                search: true,
                overHidden: true,
                sortable: true,
                width:120,
              rules: [{
                required: true,
                message: "请输入物料编号",
                trigger: "blur"
              }],
            },
              {
                  label: "物料名称",
                  prop: "materialName",
                  type: "input",
                  search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
                  rules: [{
                      required: true,
                      message: "请输入物料名称",
                      trigger: "blur"
                  }],
              },
              {
                  label: "规格",
                  prop: "materialType",
                  type: "input",
                  overHidden: true,
                  sortable: true,
                  width:120,
                  rules: [{
                      required: true,
                      message: "请输入规格",
                      trigger: "blur"
                  }],
              },
            {
              label: "客户条码",
              prop: "barCode",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
              rules: [{
                required: true,
                message: "请输入客户条码",
                trigger: "blur"
              }],
            },
            {
              label: "描述说明",
              prop: "materialDesc",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "单位",
              prop: "unit",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
              rules: [{
                required: true,
                message: "请输入单位",
                trigger: "blur"
              }],
            },
            {
              label: "数量",
              prop: "num",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
              rules: [{
                required: true,
                message: "请输入数量",
                trigger: "blur"
              }],
            },
            {
              label: "单价/100PCS",
              prop: "price",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
              rules: [{
                required: true,
                message: "请输入单价/100",
                trigger: "blur"
              }],
            },
            {
              label: "金额",
              prop: "totalPrice",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
                disabled:true,
            },
            {
              label: "送货日期",
              prop: "deliDate",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "已生产数量",
              prop: "prodNum",
              type: "input",
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "生产已完成",
              prop: "prodComplete",
              type: "input",
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "生产操作人员",
              prop: "prodCompOpr",
              type: "input",
                overHidden: true,
                sortable: true,
                width:150,
                hide:true,
            },
            {
              label: "生产已完成时间",
              prop: "prodCompDate",
              type: "input",
                overHidden: true,
                sortable: true,
                width:160,
                hide:true,
            },
            {
              label: "已入仓数量",
              prop: "stockNum",
              type: "input",
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "入仓已完成",
              prop: "stockComplete",
              type: "input",
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "已送货数量",
              prop: "deliNum",
              type: "input",
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "送货已完成",
              prop: "deliComplete",
              type: "input",
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "送货操作人员",
              prop: "deliCompOpr",
              type: "input",
                overHidden: true,
                sortable: true,
                width:160,
                hide:true,
            },
            {
              label: "送货操作时间",
              prop: "deliCompDate",
              type: "input",
                overHidden: true,
                sortable: true,
                width:160,
                hide:true,
            },
            {
              label: "仓存数量",
              prop: "totalStockNum",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "件数",
              prop: "pcsCount",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "方数",
              prop: "cubeNum",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "货代资料",
              prop: "deliInfo",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "通知方",
              prop: "noted",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
              {
                  label: "备注",
                  prop: "remark",
                  type: "textarea",
                  span: 24,
                  overHidden: true,
                  sortable: true,
                  width:120,
                  hide:true,
              },
            {
              label: "创建人",
              prop: "createUserName",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.order_add, false),
          viewBtn: this.validData(this.permission.order_view, false),
          delBtn: this.validData(this.permission.order_delete, false),
          editBtn: this.validData(this.permission.order_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
        setMaterial(data) {
          if (this.data && this.data.length > 0 && this.data.some((val) =>(data.materialNo == val.materialNo && !data.id))) {
              this.$message({
                  type: "error",
                  message: "物料已存在!"
              });
              return;
          } else {
              let ignore = false;
              this.data.filter((item, index)=>{
                  if (data.materialNo == item.materialNo) {
                      Object.assign(item, data)
                      ignore = true;
                  }
              })
              if (!ignore) {
                  data.itemNo = this.data.length + 1;
                  data.idNo = this.idNo;
                  this.data.push(data);
              }
          }
        },
        handleView(row) {
            let title="查看订单项信息";
            let type='view';
            if (row.id) {
                const id = row.id;
                this.$refs.orderItemForm.show({
                    id,
                    title,
                    type
                })
            }else {
                this.$refs.orderItemForm.showWithData({
                    row,
                    title,
                    type
                });
            }
        },
        handleEdit(row) {
            let title="编辑订单项信息";
            let type='edit';
            if (row.id) {
                const id = row.id;
                this.$refs.orderItemForm.show({
                    id,
                    title,
                    type
                })
            }else {
                this.$refs.orderItemForm.showWithData({
                    row,
                    title,
                    type
                });
            }
        },
        handleAdd() {
            if (this.customerId) {
                this.$refs.orderItemForm.customerId = this.customerId;
                let title = "创建订单项信息";
                let type = 'edit';
                this.$refs.orderItemForm.show({
                    title,
                    type
                })
            } else {
                this.$message({
                    type: "error",
                    message: "请先选择客户!"
                });
            }
        },
        setData(idNo) {
          this.idNo = idNo;
          this.onLoad(this.page);
        },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
              this.data.splice(row.$index,1);
              return;
          })
          .then(() => {
            // this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
              this.data = this.data.filter(item =>{
                  return !this.selectionList.some((val)=> (val.materialNo == item.materialNo))
              })
          })
          .then(() => {
            // this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
            console.log("@@@@@@@@@@@@@@@",this.idNo)
        this.loading = true;
        params.idNo = this.idNo;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
